<?php if (!defined('ROOT_DIR')) exit('No direct script access!');

/**
 *@author Ervin R. Musngi <ervimusngi@gmail.com>
 *@copyright Copyright (c) 2012, Ervin Musngi
 *@DateCreated - December 2, 2011
 *@DateModified - December 5, 2011
 *
 *  #Loader - Class used to load Library, helpers and views.
 *
 */

class Loader {
    
    private $_instance;
    private $_objects = array();
    
    public function __construct()
    {
        $this->_instance =& Controller::get_instance();
    }
    
    public function library( $class = '', $param = '' )
    {
        $this->_instance->$class =& load_class($class,'libraries',$param);
    }
    
    public function model( $modelName = '' , $modelAlias = '' )
    {
        if ( $modelAlias != '' && is_string($modelAlias)) {
            
            $this->_instance->$modelAlias =& load_class($modelName);
            
        } else {
            $this->_instance->$modelName =& load_class($modelName);
        }
    }
    
    public function view( $view = '' , $data = array() , $return = false )
    {
        GLOBAL $application_folder,$system_folder;
        
        if ( !file_exists(ROOT_DIR.'/'.$application_folder.'/views/'.$view.'.php') )
        {
            if ( ERROR_REPORT === true )
            {
                require_once(ROOT_DIR.'/'.$system_folder.'/error/unableToLoadView.php');
            }
            
            exit();
        }
        
        if ( $return ) {
            ob_start();
            extract($data);
            require_once(ROOT_DIR.'/'.$application_folder.'/views/'.$view.'.php');
            $content = ob_get_contents();
            ob_end_clean();
            return $content;
        } else {
            extract($data);
            require_once(ROOT_DIR.'/'.$application_folder.'/views/'.$view.'.php');
        }
    }
        
}